package org.groovymud.shell.security.loginsteps;

import java.io.IOException;

import javax.security.auth.callback.Callback;
import javax.security.auth.callback.ConfirmationCallback;
import javax.security.auth.callback.TextOutputCallback;
import javax.security.auth.callback.UnsupportedCallbackException;
import javax.security.auth.login.FailedLoginException;
import static org.groovymud.shell.security.loginsteps.CallBackDelegate.*;
import org.groovymud.shell.security.MudLoginModuleData;
import org.groovymud.shell.security.callbacks.SelfHandlingConfirmationCallback;

public class ConfirmNewPlayer extends LoginStep {

	String nameDoesNotExistPrompt = "Your name is not in our annuls: ";

    @Override
	public void execute(MudLoginModuleData data) throws IOException, UnsupportedCallbackException, FailedLoginException {
		if (data.getPlayer() == null && !data.isGuest()) {
			boolean confirmed = false;
			ConfirmationCallback confirm = new SelfHandlingConfirmationCallback(nameDoesNotExistPrompt + data.getUsername() + "..  Did you type it correctly?", ConfirmationCallback.INFORMATION, ConfirmationCallback.YES_NO_OPTION, ConfirmationCallback.YES);
			Callback[] cb = createCallbackArray(confirm);
			data.getCallbackHandler().handle(cb);
			confirmed = (confirm.getSelectedIndex() == ConfirmationCallback.YES);
			if (!confirmed) {
				TextOutputCallback toc = new TextOutputCallback(TextOutputCallback.WARNING, "Ok, try again then..");
				cb = createCallbackArray(toc);
				data.getCallbackHandler().handle(cb);
				getFirstStep().execute(data);
				return;
			}
		}
		if (getNextStep() != null) {
			getNextStep().execute(data);
		}
	}

	/**
	 * @return the nameDoesNotExistPrompt
	 */
	public String getNameDoesNotExistPrompt() {
		return nameDoesNotExistPrompt;
	}

	/**
	 * @param nameDoesNotExistPrompt the nameDoesNotExistPrompt to set
	 */
	public void setNameDoesNotExistPrompt(String nameDoesNotExistPrompt) {
		this.nameDoesNotExistPrompt = nameDoesNotExistPrompt;
	}

}
